package adventure.commands;

import adventure.*;
import adventure.placeable.Fightable;
import player.*;
import adventure.placeable.Mobile;
import colour.ColourUtil;

/**
 *
 * @author Michael Hanns
 *
 */
public class Kill extends AbstractCommand {

    public Kill(Adventure a) {
        super(a, new String[]{"KILL", "ATTACK"});
    }

    @Override
    public String validateAndExecute(Player player, String[] args) {
        if (args.length == 1) {
            return "Kill what?";
        } else {
            if (((AdventureEdit) adv).getRoomManager().isLightPresent(
                    player.x(), player.y(), player.z())) {
                return kill(player, Util.consolidateArguments(args));
            } else {
                return "You cannot see anything in the darkness!";
            }
        }
    }

    private String kill(Player player, String target) {
        int x = player.x();
        int y = player.y();
        int z = player.z();

        Mobile mob = rooms.getRoomMobile(x, y, z, target);
        if (mob.getID() > 0) {
            return killMobile(player, mob);
        } else {
            int targetPlayer = rooms.getRoomPlayer(x, y, z, target);
            if (player.getID() == targetPlayer) {
                return "You cannot fight yourself!";
            } else if (targetPlayer > 0) {
                return killPlayer(player, targetPlayer);
            } else {
                return "You don't see any " + target + "!";
            }
        }
    }

    private String killMobile(Player player, Mobile mob) {
        String output = "";

        if (player.attack((Fightable)mob)) {
            output += "\nYou attack "
                    + ColourUtil.colourise(mob.getAliveName(), ColourUtil.red) + "!";
            out.broadcastLocalToPlayer(player,
                    ColourUtil.colourise(player.getName(), ColourUtil.magenta)
                    + " attacks "
                    + ColourUtil.colourise(mob.getAliveName(), ColourUtil.red) + "!");
        } else {
            output += "\nYou are already fighting "
                    + ColourUtil.colourise(mob.getAliveName(), ColourUtil.red) + "!";
        }

        output += "\n";

        return output;
    }

    private String killPlayer(Player player, int targetPlayer) {
        if (players.getOnlinePlayer(targetPlayer).pvpImmune()) {
            return "\nYou cannot attack another player so recently slain.";
        } else {
            System.out.println("Into killPlayer, not immune.");
            String target = players.getOnlinePlayer(targetPlayer).getName();
            System.out.println("Target identified.");
            if (player.attack((Fightable) players.getOnlinePlayer(targetPlayer))) {
                out.broadcastToPlayer(players.getOnlinePlayer(targetPlayer),
                        ColourUtil.colourise(player.getName(), ColourUtil.magenta)
                        + " has attacked you!");
                System.out.println("Target notified.");
                players.getOnlinePlayer(targetPlayer).attack((Fightable) player);
                System.out.print("Player attacked.");
                
                return "\nYou attack "
                        + ColourUtil.colourise(target, ColourUtil.magenta) + "!";
            } else {
                return "\nYou are already fighting "
                        + ColourUtil.colourise(target, ColourUtil.magenta) + "!";
            }
        }
    }
}
